package com.lookme.lmtool.cracker.spliter;

import com.lookme.lmtool.cracker.annotation.ParamAnnotation;
import com.lookme.lmtool.cracker.annotation.SpliterAnnotation;
import com.lookme.lmtool.model.Rect;
import com.lookme.lmtool.model.SpliterInfo;
import com.lookme.lmtool.utils.ImageUtils;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
import lombok.Getter;
import lombok.Setter;

import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
@Getter@Setter
@SpliterAnnotation("固定分割")
public class FixedSpliter extends AbstractSpliter {

    @XStreamAsAttribute
    @ParamAnnotation(name = "最小的联通点", desc = "最小的联通点",inputType = ParamAnnotation.InputType.Spinner,min =1,max=20)
    int splitSize=4;

    @XStreamAsAttribute
    @ParamAnnotation(name = "去空白边", desc = "去空白边",inputType = ParamAnnotation.InputType.Radio,width = 45)
    boolean removeBlank=false;

    @Override
    public List<SpliterInfo> split(BufferedImage src) {
        List<SpliterInfo> subImgs = new ArrayList<>();
        int width = src.getWidth();
        int height = src.getHeight();

        int ew=width/splitSize;
        for(int i=0;i<splitSize;i++){
            Rect rect= Rect.builder().x(i*ew).y(0).width(ew).height(height).build();
            if(removeBlank) {
                rect = ImageUtils.getRemoveBlankRect(src, rect);
            }
            subImgs.add(SpliterInfo.get(src,rect));
        }
        return subImgs;
    }
}
